<!--
  * 投诉查询
-->
<template>
  <!---------- 查询表单form begin ----------->
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-col :span="5">
        <a-form-item label="投诉订单号" class="smart-query-form-item">
          <a-input v-model:value="queryForm.complaintNo" placeholder="输入投诉订单号" />
        </a-form-item>
      </a-col>
      <a-col :span="6">
        <a-form-item label="投诉日期" class="smart-query-form-item">
          <a-range-picker class="w-full" v-model:value="queryForm.complaintTime" :valueFormat="'YYYY-MM-DD'" @change="changeComplaintTime" />
        </a-form-item>
      </a-col>
      <a-col :span="5">
        <a-form-item label="支付通道" class="smart-query-form-item">
          <CustomSelect
            v-model:value="queryForm.channelIdList"
            :placeholder="'请选择支付通道'"
            :maxTagCount="1"
            :maxTagTextLength="5"
            :api="channelApi.queryPage"
            value-field="id"
            label-field="channelName"
            :mode="'multiple'"
          />
        </a-form-item>
      </a-col>
      <a-col :span="5">
        <a-form-item label="投诉状态" class="smart-query-form-item">
          <a-select
            v-model:value="queryForm.statusList"
            :mode="'multiple'"
            :maxTagCount="1"
            :maxTagTextLength="5"
            :options="COMPLAINT_STATUS"
            placeholder="请选择投诉状态"
          />
        </a-form-item>
      </a-col>
      <!-- <a-col :span="3">
        <a-form-item label="处理结果" class="smart-query-form-item">
          <a-select v-model:value="queryForm.businessType" allowClear :options="CHANNEL_BUSINESS_TYPE" placeholder="请选择处理结果" />
        </a-form-item>
      </a-col> -->
      <!-- <a-col :span="3">
        <a-form-item label="签约日期" class="smart-query-form-item">
          <a-date-picker :allowClear="false" :inputReadOnly="true" v-model:value="queryForm.fTime" />
        </a-form-item>
      </a-col> -->
      <!-- 
      <a-form-item label="状态" class="smart-query-form-item">
        <SmartEnumSelect width="200px" v-model:value="queryForm.status" enum-name="STATUS_ENUM" placeholder="状态" />
      </a-form-item> 
      -->
      <a-col :span="5">
        <a-form-item class="smart-query-form-item">
          <a-button type="primary" @click="onSearch">
            <template #icon>
              <SearchOutlined />
            </template>
            查询
          </a-button>
          <a-button @click="resetQuery" class="smart-margin-left10">
            <template #icon>
              <ReloadOutlined />
            </template>
            重置
          </a-button>
        </a-form-item></a-col
      >
    </a-row>
  </a-form>
  <!---------- 查询表单form end ----------->

  <a-card size="small" :bordered="false" :hoverable="true">
    <!---------- 表格操作行 begin ----------->
    <a-row class="smart-table-btn-block">
      <div class="smart-table-title">数据列表</div>
      <!-- <div class="smart-table-operate-block">
      </div> -->
      <div class="smart-table-setting-block">
        <!-- <a-button @click="showForm" type="primary" size="small" v-privilege="'trade:insert'">
          <template #icon>
            <PlusOutlined />
          </template>
          添加
        </a-button> -->
        <!-- <TableOperator v-model="columns" :tableId="null" :refresh="queryData" /> -->
      </div>
    </a-row>
    <!---------- 表格操作行 end ----------->

    <!---------- 表格begin ----------->
    <a-table size="small" :dataSource="tableData" :columns="columns" rowKey="id" bordered :loading="tableLoading" :pagination="false">
      <template #bodyCell="{ record, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate" v-if="record.source == 1">
            <!-- 微信操作 -->
            <a-button @click="handle(record)" type="link">处理</a-button>
            <a-button @click="handleCompleted(record)" type="link">处理完结</a-button>
            <a-button @click="view(record)" type="link">详情</a-button>
            <!-- <a-button @click="completed(record)" type="link">退款</a-button> -->
            <!--  v-privilege="'trade:update'" -->
          </div>
          <div class="smart-table-operate" v-if="record.source == 3">
            <!-- 支付宝小程序操作 -->
            <a-button @click="handleAliMini(record)" type="link">处理</a-button>
            <a-button @click="replyAliMini(record, 'supplement')" type="link">补充凭证</a-button>
            <a-button @click="replyAliMini(record, 'reply')" type="link">留言</a-button>
            <a-button @click="viewAliMini(record)" type="link">详情</a-button>
            <!--  v-privilege="'trade:update'" -->
          </div>
          <div class="smart-table-operate" v-if="record.source == 2">
            <!-- 支付宝操作 -->
            <a-button @click="handleAli(record)" type="link">处理</a-button>
            <a-button @click="replyAli(record, 'reply')" type="link">留言</a-button>
            <a-button @click="viewAli(record)" type="link">详情</a-button>
            <!--  v-privilege="'trade:update'" -->
          </div>
        </template>
      </template>
    </a-table>

    <!---------- 表格 end ----------->

    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :page_sizeOptions="PAGE_SIZE_OPTIONS"
        :defaultpage_size="queryForm.pageSize"
        v-model:current="queryForm.pageNo"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryData"
        @showSizeChange="queryData"
        :show-total="(total) => `共${total}条`"
      />
    </div>
  </a-card>

  <WeChatReplyForm ref="weChatForm" v-model:open="replyOptions.open" :row-data="replyOptions.rowData" />
  <WeChatDetail v-model:open="detailOptions.open" :row-data="detailOptions.rowData" />
  <AliFeedbackForm v-model:open="aliFeedbackOptions.open" :row-data="aliFeedbackOptions.rowData" :form-source="aliFeedbackOptions.formSource" />
  <AliReplyForm
    v-model:open="aliReplyOptions.open"
    :row-data="aliReplyOptions.rowData"
    :api-type="aliReplyOptions.apiType"
    :form-source="aliReplyOptions.formSource"
  />
  <AliDetail v-model:open="detailAliOptions.open" :row-data="detailAliOptions.rowData" :formSource="detailAliOptions.formSource" />
</template>
<script setup>
  import { reactive, ref, onMounted, getCurrentInstance } from 'vue'
  import { message, Modal } from 'ant-design-vue'
  import { getComplaintPage, wechatComplete } from '/@/api/business/b-merchant/complaintManage'
  import { PAGE_SIZE_OPTIONS } from '/@/constants/common-const'
  import { smartSentry } from '/@/lib/smart-sentry'
  import TableOperator from '/@/components/support/table-operator/index.vue'
  import CustomSelect from '/@/components/business/comment/CustomSelect.vue'
  import dayjs from 'dayjs'
  import WeChatReplyForm from '/@/components/business/comment/complaint/wechat-reply-form.vue'
  import WeChatDetail from '/@/components/business/comment/complaint/wechat-detail.vue'
  import AliFeedbackForm from '/@/components/business/comment/complaint/ali-feedback-form.vue'
  import AliReplyForm from '/@/components/business/comment/complaint/ali-reply-form.vue'
  import AliDetail from '/@/components/business/comment/complaint/ali-detail.vue'
  // import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue'
  import { channelApi } from '/@/api/business/b-operation/channel/channel-api'
  import { selectDictLabel, formatToMoney, strToArr } from '/@/utils/common'
  import {
    COMPLAINT_STATUS, //投诉状态
    COMPLAINT_SOURCE, //投诉来源
    COMPLAINT_FEEDBACK_CODE, //投诉处理结果
  } from '/@/constants/dict'
  import { cloneDeep } from 'lodash-es'
  import { myUpload } from '/@/utils/myUpload'

  const { proxy } = getCurrentInstance()

  // ---------------------------- 表格列 ----------------------------
  const columns = ref([
    {
      title: '序号',
      dataIndex: 'index',
      width: 60,
      customRender: ({ index }) => {
        return (queryForm.pageNo - 1) * queryForm.pageSize + index + 1
      },
    },
    {
      title: '来源',
      dataIndex: 'source',
      customRender: ({ text }) => {
        return selectDictLabel(COMPLAINT_SOURCE, text)
      },
      ellipsis: true,
    },
    {
      title: '投诉订单号',
      dataIndex: 'complaintNo',
      ellipsis: true,
    },
    {
      title: '投诉时间',
      dataIndex: 'complaintTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '投诉状态',
      dataIndex: 'status',
      customRender: ({ text }) => {
        return selectDictLabel(COMPLAINT_STATUS, text)
      },
      ellipsis: true,
    },
    {
      title: '过期时间',
      dataIndex: 'overdueTime',
      customRender: (column) => dayjs(column.text).format('YYYY-MM-DD HH:mm:ss'),
      ellipsis: true,
    },
    {
      title: '交易订单号',
      dataIndex: 'orderIds',
      ellipsis: true,
    },
    {
      title: '用户联系方式',
      dataIndex: 'phoneNo',
      ellipsis: true,
    },
    {
      title: '投诉诉求',
      dataIndex: 'leafCategoryName',
      ellipsis: true,
    },
    {
      title: '投诉原因',
      dataIndex: 'complainReason',
      ellipsis: true,
    },
    {
      title: '投诉内容',
      dataIndex: 'content',
      ellipsis: true,
    },
    // {
    //   title: '用户回复',
    //   dataIndex: 'companyName',
    //   ellipsis: true,
    // },
    {
      title: '处理结果',
      dataIndex: 'companyName',
      customRender: ({ text }) => {
        return selectDictLabel(COMPLAINT_FEEDBACK_CODE, text)
      },
      ellipsis: true,
    },
    {
      title: '通道类型',
      dataIndex: 'channelTypes',
      customRender: ({ text }) => {
        return proxy.$smartEnumPlugin.getDescByValue('ZZB_CHANNEL_TYPE_ENUM', text)
        // return selectDictLabel(COMPLAINT_FEEDBACK_CODE, text)
      },
      ellipsis: true,
    },
    {
      title: '退款金额',
      dataIndex: 'refundAmt',
      customRender: (column) => (column.text ? formatToMoney(column.text / 100) : ''),
      ellipsis: true,
    },
    // {
    //   title: '处理结果',
    //   dataIndex: 'chargeType',
    //   customRender: ({ text }) => {
    //     return selectDictLabel(COMPLAINT_STATUS, text)
    //   },
    //   ellipsis: true,
    // },
    {
      title: '操作',
      dataIndex: 'action',
      fixed: 'right',
      width: 200,
    },
  ])
  // ---------------------------- 查询数据表单和方法 ----------------------------
  const changeComplaintTime = (val) => {
    let value = strToArr(val)
    queryForm.complaintTimeBegin = value[0] ? value[0] : undefined
    queryForm.complaintTimeEnd = value[1] ? value[1] : undefined
  }
  const queryFormState = {
    complaintNo: undefined, //投诉订单号
    complaintTime: [dayjs().startOf('day'), dayjs().endOf('day')], //投诉日期
    complaintTimeEnd: dayjs().endOf('day').valueOf(), //投诉日期
    complaintTimeBegin: dayjs().startOf('day').valueOf(), //投诉日期
    merchantIdList: [], //投诉商户
    channelIdList: [], //支付通道ids
    statusList: [], //投诉状态
    pageNo: 1,
    pageSize: 10,
  }

  // 查询表单form
  const queryForm = reactive({ ...queryFormState })
  // 表格加载loading
  const tableLoading = ref(false)
  // 表格数据
  const tableData = ref([])
  // 总数
  const total = ref(0)

  // 重置查询条件
  function resetQuery() {
    let pageSize = queryForm.pageSize
    Object.assign(queryForm, queryFormState)
    queryForm.pageSize = pageSize
    queryData()
  }

  // 搜索
  function onSearch() {
    queryForm.pageNo = 1
    queryData()
  }

  // 查询数据
  async function queryData() {
    tableLoading.value = true
    try {
      let queryResult = await getComplaintPage(queryForm)
      tableData.value = queryResult.data
      total.value = queryResult.total
    } catch (e) {
      smartSentry.captureError(e)
    } finally {
      tableLoading.value = false
    }
  }

  onMounted(queryData)

  /* 查看详情 */
  const detailOptions = ref({
    open: false,
    data: {},
  })
  function view(row) {
    detailOptions.value.open = true
    detailOptions.value.rowData = row
  }
  /* 查看详情 */

  // 回复
  const replyOptions = ref({
    open: false,
    data: {},
  })
  const weChatType = ref('reply')
  // 回复
  function handle(row, type) {
    replyOptions.value.open = true
    replyOptions.value.rowData = row
    weChatType.value = type
    // openDetail.value = true
  }
  // 办结
  function handleCompleted(row) {
    Modal.confirm({
      title: '提示',
      content: '请再次确认是否投诉已妥善处理!',
      okText: '确定',
      okType: 'danger',
      async onOk() {
        const { complaintNo, merchantId, channelIds } = row
        const data = { complaintId: complaintNo, merchantId, channelId: channelIds }
        wechatComplete(data).then((res) => {
          if (res.code === 200) {
            message.success('投诉已办结')
            queryData()
          } else {
            message.error(res.msg)
          }
        })
      },
      cancelText: '取消',
      onCancel() {},
    })
  }

  /* Ali小程序操作 */
  const aliFeedbackOptions = ref({
    open: false,
    rowData: null,
    formSource: null,
  })
  function handleAliMini(row) {
    aliFeedbackOptions.value = { open: true, rowData: row }
  }
  function handleAli(row) {
    aliFeedbackOptions.value = { open: true, rowData: row, formSource: 'ali' }
  }
  const aliReplyOptions = ref({
    open: false,
    rowData: null,
    apiType: null,
    formSource: null,
  })
  function replyAliMini(row, type) {
    aliReplyOptions.value = { open: true, rowData: row, apiType: type }
  }
  function replyAli(row, type) {
    aliReplyOptions.value = { open: true, rowData: row, apiType: type, formSource: 'ali' }
  }
  const detailAliOptions = ref({
    open: false,
    rowData: null,
    formSource: null,
  })
  function viewAliMini(row) {
    detailAliOptions.value = { open: true, rowData: row }
  }
  function viewAli(row) {
    detailAliOptions.value = { open: true, rowData: row, formSource: 'ali' }
  }
  /* Ali小程序操作 */
</script>
